package com.wy.self;

import java.io.FileReader;
import java.io.FileWriter;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;

import com.wy.base.AbstractWizard;

/**
 * 自定义模板实现类
 *
 * @author 飞花梦影
 * @date 2021-07-19 14:03:03
 * @git {@link https://github.com/dreamFlyingFlower }
 */
public class SelfWizard extends AbstractWizard {

	private String templateUri = "";

	public static Boolean isMavenType;

	public static Boolean isNone = true;

	/**
	 * Adding the page to the wizard.
	 */
	@Override
	public void addPages() {
		// page = new RestControllerPage(selection);
		// addPage(page);
		setPage(new SelfWizardPage(this.getSelection()));
		addPage(getPage());
	}

	@Override
	public boolean performFinish() {
		return super.performFinish("/templates/" + templateUri);
	}

	/**
	 * The worker method. It will find the container, create the file if missing or
	 * just replace its contents, and open the editor on the newly created file.
	 * 
	 * @throws Exception
	 */
	@Override
	public void doFinish(String containerName, String fileName, String packageName, IProgressMonitor monitor,
			String templateUri) throws Exception {
		// create a sample file
		monitor.beginTask("Creating " + fileName, 2);
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = root.findMember(new Path(containerName));
		if (!resource.exists() || !(resource instanceof IContainer)) {
			throwCoreException("Container \"" + containerName + "\" does not exist.");
		}
		IContainer container = (IContainer) resource;
		IJavaProject project = (IJavaProject) container.getProject().getNature("org.eclipse.jdt.core.javanature");
		IPackageFragment ipackage =
				project.getAllPackageFragmentRoots()[0].createPackageFragment(packageName, false, monitor);
		String contents = loadTemplate(containerName, templateUri);
		ipackage.createCompilationUnit(fileName + ".java", contents, false, monitor);
		if (!isNone) {
			addDependency(isMavenType);
		}
		monitor.worked(1);
	}

	public String getTemplateUri() {
		return templateUri;
	}

	public void setTemplateUri(String templateUri) {
		this.templateUri = templateUri;
	}

	private void addDependency(Boolean isMavenType) {
		if (isMavenType) {
			Model model = null;
			FileReader reader = null;
			FileWriter writer = null;
			MavenXpp3Reader mavenreader = new MavenXpp3Reader();
			MavenXpp3Writer mavenwriter = new MavenXpp3Writer();
			try {
				reader = new FileReader("pom.xml");
				model = mavenreader.read(reader);
				Dependency springweb = new Dependency();
				springweb.setGroupId("org.springframework");
				springweb.setArtifactId("spring-web");
				springweb.setVersion("4.3.7.RELEASE");
				model.getDependencies().add(springweb);
				writer = new FileWriter("pom.xml");
				mavenwriter.write(writer, model);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}